package cn.itsource.xhy.pro.service.impl;

import cn.itsource.xhy.basic.service.impl.BaseServiceImpl;
import cn.itsource.xhy.pro.domain.ProDetail;
import cn.itsource.xhy.pro.domain.Product;
import cn.itsource.xhy.pro.mapper.ProDetailMapper;
import cn.itsource.xhy.pro.mapper.ProductMapper;
import cn.itsource.xhy.pro.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProDetailMapper proDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void save(Product product) {
        product.setState(0);
        super.save(product);
        // 获取detail对象
        ProDetail detail = product.getDetail();
        // 设置proId
        detail.setProduct_id(product.getId());
        // 保存
        proDetailMapper.save(detail);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        // 获取detail对象
        ProDetail detail = product.getDetail();
        // 设置proId
        detail.setProduct_id(product.getId());
        // 保存
        proDetailMapper.update(detail);
    }

    @Override
    public void del(Long id) {
        // 获取product对象
        Product product = productMapper.findByProId(id);
        // 获取detail对象
        ProDetail detail = product.getDetail();
        // 通过id删除
        proDetailMapper.del(detail.getId());
        //删除
        super.del(id);
    }

    @Override
    public void onSale(Long[] ids) {
        productMapper.onSale(ids);
    }

    @Override
    public void downSale(Long[] ids) {
        productMapper.downSale(ids);
    }

    @Override
    public Product findByProId(Long id) {
        return productMapper.findByProId(id);
    }
}
